﻿using System.Collections.Generic;
using System.Web;
using NExt.Handler.Base.Default;

namespace NExt.Handler.Base
{
    public abstract class AbstractChainedHandlerFactory : IHttpHandlerFactory
    {
        private readonly IList<IHandlerFactory> httpHandlerFactoryList;

        protected AbstractChainedHandlerFactory()
        {
            httpHandlerFactoryList = BuildHandlerFactoryList();
        }

        protected abstract IList<IHandlerFactory> BuildHandlerFactoryList();

        public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
        {
            foreach (IHandlerFactory handlerFactory in httpHandlerFactoryList)
            {
                bool canHandle = handlerFactory.CanHandle(context, requestType, url, pathTranslated);
                if (!canHandle) continue;
                IHttpHandler httpHandler = handlerFactory.GetHandler(context, requestType, url, pathTranslated);
                return httpHandler;
            }

            return null;
        }

        public void ReleaseHandler(IHttpHandler handler)
        {
        }
    }
}
